package pers.acp.test.application.test

import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.acp.boot.socket.base.UdpServerHandle
import io.netty.channel.ChannelHandlerContext
import org.noear.snack4.ONode
import org.noear.solon.annotation.Component
import org.noear.solon.annotation.Inject
import pers.acp.test.application.mapper.TableOneMapper

/**
 * @since JDK 17
 */
@Component("testUdpHandle")
class TestUdpHandle(
    private val log: LogAdapter,
    @param:Inject private val tableOneMapper: TableOneMapper
) : UdpServerHandle() {
    @Throws(Exception::class)
    override fun doProcess(receiveStr: String): String {
        log.info("收到报文：$receiveStr")
        val response = ONode.serialize(tableOneMapper.selectAll()).toString()
        log.info("返回：$response")
        return response
    }

    override fun userEventTriggered(event: Any): String? {
        return null
    }

    override fun exceptionCaught(context: ChannelHandlerContext, cause: Throwable) {
        log.error("异常：" + cause.message)
        context.close()
    }
}
